package collector

import (
	"custom-oracledb-exporter/config"
	"database/sql"
	"log"

	"github.com/prometheus/client_golang/prometheus"
)

var metrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
	Namespace: "custom_oracledb",                       // 指标命名空间
	Name:      "custom_oracledb_password_expired_days", // 指标名称
	Help:      "An gauge metric.",                      // 指标帮助信息
},
	// 自定义标签
	[]string{"username", "database"},
)

func init() {
	// 注册指标
	prometheus.MustRegister(metrics)
}

func PasswdExpiredMetrics(DB *sql.DB) {
	rows, err := DB.Query(`
		SELECT
			username,
			trunc( expiry_date ) - trunc( SYSDATE ) AS dayNums
		FROM
			dba_users
		WHERE
		    account_status='OPEN' and trunc( expiry_date ) - trunc( SYSDATE ) >= 0
	`)
	if err != nil {
		log.Fatal(err.Error())
	}
	// 遍历结果集
	for rows.Next() {
		var (
			username string
			daynum   int
		)
		if err := rows.Scan(&username, &daynum); err != nil {
			log.Fatal(err)
		}
		//更新指标
		metrics.With(prometheus.Labels{"username": username, "database": config.C.Database.ServiceName}).Set(float64(daynum))
		// log.Println("username:", username, "daynum:", daynum)
	}
}
